package org.zizi.util;

public class RequestParamReplace
{
	/** 将原来的source中的参数 param 的值替换成 value。但是如果不存在param，则添加 */
	public static String replace(String source, String param, String value)
	{
		if (source == null || source.isEmpty())
		{
			source = param + "=" + value;
		}
		else
		{
			int b = source.indexOf(param);
			if (b < 0)
			{
				source = source + "&" + param + "=" + value;
			}
			else
			{
				b = b + param.length() + 1;
				int e = source.indexOf("&", b);
				if (e < 0)
				{
					source = source.substring(0, b) + value;
				}
				else
				{
					source = source.substring(0, b) + value + source.substring(e);
				}
			}
		}
		
		return "?" + source;
	}
}
